home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / Hydra11s.lha / HBBS / Source / Utils / LastCallers / Main.C < prev    next >
C/C++ Source or Header  |  1996-11-05  |  15KB  |  514 lines

  1.  
  2. #include <exec/types.h>
  3. #include <exec/memory.h>
  4. #include <dos/dos.h>
  5. #include <clib/exec_protos.h>
  6. #include <clib/dos_protos.h>
  7. #include <clib/alib_protos.h>
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <time.h>
  14. #include <math.h>
  15.  
  16. #ifdef __SASC
  17. int CXBRK(void) { return(0); }
  18. int _CXBRK(void) { return(0); }
  19. void chkabort(void) {}
  20. #endif
  21.  
  22. #include <HBBS/ANSI_Codes.h>
  23. #include <HBBS/Defines.h>
  24. #include <HBBS/types.h>
  25. #include <HBBS/structures.h>
  26. #include <HBBS/hbbscommon_protos.h>
  27. #include <HBBS/hbbscommon_pragmas.h>
  28. #include <HBBS/Hbbsnode_protos.h>
  29. #include <HBBS/Hbbsnode_pragmas.h>
  30.  
  31. /* hbbs variables */
  32. struct Library *HBBSCommonBase=NULL;
  33. struct BBSGlobalData *BBSGlobal=NULL;
  34. char outstr[1024];
  35.  
  36.  
  37. /* lastcaller variables */
  38.  
  39. struct CallerNode
  40. {
  41.   struct Node node; // ->ln_Name contains the handle..
  42.   char Group[LEN_GROUP];
  43.   char OnTime[LEN_TIMESTR];
  44.   char OffTime[LEN_TIMESTR];
  45.   char Date[LEN_DATESTR];
  46.   char Actions[LEN_ACTIONS];
  47.   char Speed[MAX_CPSBAUD_LEN+1];
  48.   V_BIGNUM NodeNum;
  49.  
  50.   V_BIGNUM FilesUploaded;
  51.   V_BIGNUM FilesDownloaded;
  52.   V_BIGNUM BytesUploaded;
  53.   V_BIGNUM BytesDownloaded;
  54.  
  55. };
  56.  
  57. struct List *CallerList=NULL;
  58.  
  59.  
  60.  
  61. static VOID cleanup(ULONG num)
  62. {
  63.   if (HBBSCommonBase)
  64.   {
  65.     HBBS_CleanUpCommon();
  66.     CloseLibrary (HBBSCommonBase);
  67.   }
  68.  
  69.   if (num) printf("Door Error = %d\n",num);
  70.  
  71.   exit(0);
  72. }
  73.  
  74. static VOID init(char *name)
  75. {
  76.   if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  77.   {
  78.     cleanup(1);
  79.   }
  80.  
  81.   if (!(HBBS_InitCommon()))
  82.   {
  83.     cleanup(2);
  84.   }
  85.  
  86.   SetProgramName(name);
  87. }
  88.  
  89. void DisplayHelp( void )
  90. {
  91.   puts("LastCallers util for HydraBBS - ()C 1996 Hydra/LSD - Deluxe Software Ltd.");
  92.   puts("usage: LastCallers [UPDATE <nodenum>] [[CREATE <filename> <users> <nodenum>|ALLNODES [IncludeLOCAL]]");
  93.  
  94.   puts("you should add a line like:\n"
  95.        "LastCallers UPDATE CREATE HBBS:Nodes/Node[NodeNum]/Screens/Special/LastCallers.TXT [NodeNum] 20\n"
  96.        "to the LogOffGlobal script..");
  97.   puts("you must specify UPDATE in the command line so that lastcaller can add the current\n"
  98.        "users details to it's datafile...");
  99.   puts("you can ,of course,create more than one output file, just don't specify the\n"
  100.        "UPDATE keyword in subsequent calls to LastCallers in the logoff script..\n");
  101. }
  102.  
  103. void FHWriteStr(BPTR FH,char *str)
  104. {
  105.   Write(FH,str,strlen(str));
  106.   Write(FH,"\n",1);
  107. }
  108.  
  109. void FHWriteStrNOCR(BPTR FH,char *str)
  110. {
  111.   Write(FH,str,strlen(str));
  112. }
  113.  
  114. void CreateFile(BOOL NoLocal,BOOL ScanAll,LONG OnlyNode,LONG Users,char *filename)
  115. {
  116.  
  117.   // sort the list and save the first <Users> nodes into <filename>
  118.  
  119.   LONG Callers,loop,found=0;
  120.   char tmpstr[1024],activitystring[80],ulstr[30],dlstr[30],speedstr[10];
  121.   struct CallerNode *Caller;
  122.   BPTR FH;
  123.  
  124.   if (FH=Open(filename,MODE_NEWFILE))
  125.   {
  126.  
  127.     FHWriteStr(FH,"H [ HAMFETAMiN V1.00 ]   by Hydra / LSD 1996     [ Utopia +44 (0)1202 434121 ] \n");
  128.     FHWriteStr(FH,"N User Name          Group             TimeOnBoard ACTVT U#/Byte D#/Byte Speed ");
  129.     FHWriteStr(FH,"- ------------------ ----------------- ----------- ----- ------- ------- -----");
  130.  
  131.  
  132.     Callers=HBBS_NodesInList(CallerList);
  133.  
  134.     for (loop=0;loop<Callers && found<Users;loop++)
  135.     {
  136.       Caller=(struct CallerNode *)GetNode(CallerList,loop);
  137.  
  138.       if ((ScanAll || Caller->NodeNum==OnlyNode) && ((!(NoLocal && stricmp(Caller->Speed,"LOCAL")==0)) || (!NoLocal)))
  139.       {
  140.         found++;
  141.  
  142.         activitystring[0]=0;
  143.         if (strchr(Caller->Actions,'L')) strcat(activitystring,"C"); else strcat(activitystring,"-");
  144.         if (strchr(Caller->Actions,'N')) strcat(activitystring,"N"); else strcat(activitystring,"-");
  145.         if (strchr(Caller->Actions,'H')) strcat(activitystring,"H"); else strcat(activitystring,"-");
  146.         if (strchr(Caller->Actions,'P')) strcat(activitystring,"P"); else strcat(activitystring,"-");
  147.         if (strchr(Caller->Actions,'S')) strcat(activitystring,"S"); else strcat(activitystring,"-");
  148.  
  149.         if (!(Caller->BytesUploaded))
  150.         {
  151.           strcpy(ulstr,"----");
  152.         }
  153.         else
  154.         {
  155.           if (Caller->BytesUploaded>=(1024 * 1024)) // 1MB+
  156.           {
  157. //            if (Caller->BytesUploaded>=(10 * (1024 * 1024))) // 10MB+
  158. //            {
  159.               sprintf(ulstr,"%3.0f",(float)(Caller->BytesUploaded / (1024 * 1024)));
  160. //            }
  161. //            else sprintf(ulstr,"%1.1f",(float)(Caller->BytesUploaded / (1024 * 1024)));
  162.             strcat(ulstr,"M");
  163.           }
  164.           else
  165.           {
  166.             if (Caller->BytesUploaded>(10 * 1024)) // 10K+
  167.             {
  168.               sprintf(ulstr,"%3.0f",(float)(Caller->BytesUploaded / 1024));
  169.             }
  170.             else sprintf(ulstr,"%1.1f",(float)(Caller->BytesUploaded / 1024));
  171.             strcat(ulstr,"K");
  172.           }
  173.           replace(ulstr,ulstr," ","0");
  174.         }
  175.  
  176.         if (!(Caller->BytesDownloaded))
  177.         {
  178.           strcpy(dlstr,"----");
  179.         }
  180.         else
  181.         {
  182.           if (Caller->BytesDownloaded>(1024 * 1024))
  183.           {
  184.             if (Caller->BytesDownloaded>( 10 * (1024 * 1024)))
  185.             {
  186.               sprintf(dlstr,"%3.0f",(float)(Caller->BytesDownloaded / (1024 * 1024)));
  187.             }
  188.             else sprintf(dlstr,"%1.1f",(float)(Caller->BytesDownloaded / (1024 * 1024)));
  189.             strcat(dlstr,"M");
  190.           }
  191.           else
  192.           {
  193. //            if (Caller->BytesDownloaded>=(10 * 1024))
  194. //            {
  195.               sprintf(dlstr,"%3.0f",(float)(Caller->BytesDownloaded / 1024));
  196. //            }
  197. //            else sprintf(dlstr,"%1.1f",(float)(Caller->BytesDownloaded / 1024));
  198.             strcat(dlstr,"K");
  199.           }
  200.           replace(dlstr,dlstr," ","0");
  201.         }
  202.  
  203.         sprintf(speedstr,"%5s",Caller->Speed);
  204.         replace(speedstr,speedstr," ","0");
  205.  
  206.         //                    node       handle       group   ontimeofftime speed  activity
  207.         sprintf(tmpstr,"%1.1ld %-18.18s %-17.17s %-5.5s-%-5.5s %s %02ld:%s %02ld:%s %-5.5s ",
  208.                 Caller->NodeNum,Caller->node.ln_Name,Caller->Group,Caller->OnTime,Caller->OffTime,activitystring,
  209.                 Caller->FilesUploaded,ulstr,
  210.                 Caller->FilesDownloaded,dlstr,speedstr);
  211.  
  212.         FHWriteStr(FH,tmpstr);
  213.       }
  214.     }
  215.  
  216.     FHWriteStr(FH,"------------------------------------------------------------------------------");
  217.     Close(FH);
  218.   }
  219.   else
  220.   {
  221.     puts("could not write file!");
  222.   }
  223. }
  224.  
  225.  
  226. void SaveDataFile( void )
  227. {
  228.   BPTR FH;
  229.   struct CallerNode *Caller;
  230.   char tmpstr[20];
  231.  
  232.   if (FH=Open("Progdir:LastCallers.DATA",MODE_NEWFILE))
  233.   {
  234.     for (Caller=(struct CallerNode*)CallerList->lh_Head;Caller->node.ln_Succ;Caller=(struct CallerNode*)Caller->node.ln_Succ)
  235.     {
  236.       FHWriteStr(FH,"!DATA!");
  237.       FHWriteStr(FH,Caller->node.ln_Name);
  238.       FHWriteStr(FH,Caller->Group);
  239.       FHWriteStr(FH,Caller->OnTime);
  240.       FHWriteStr(FH,Caller->OffTime);
  241.       FHWriteStr(FH,Caller->Date);
  242.       FHWriteStr(FH,Caller->Actions);
  243.       FHWriteStr(FH,Caller->Speed);
  244.       sprintf(tmpstr,"%ld",Caller->NodeNum);
  245.       FHWriteStr(FH,tmpstr);
  246.  
  247.       sprintf(tmpstr,"%ld",Caller->FilesUploaded);
  248.       FHWriteStr(FH,tmpstr);
  249.  
  250.       sprintf(tmpstr,"%ld",Caller->FilesDownloaded);
  251.       FHWriteStr(FH,tmpstr);
  252.  
  253.       sprintf(tmpstr,"%ld",Caller->BytesUploaded);
  254.       FHWriteStr(FH,tmpstr);
  255.  
  256.       sprintf(tmpstr,"%ld",Caller->BytesDownloaded);
  257.       FHWriteStr(FH,tmpstr);
  258.     }
  259.     Close(FH);
  260.   }
  261. }
  262.  
  263. BOOL ReadDataFile(LONG Users)
  264. {
  265.   char buffer[1024];
  266.   BPTR FH;
  267.   BOOL retval=FALSE,eof=FALSE,error=FALSE;
  268.   struct CallerNode *Caller;
  269.  
  270.   if (FH=Open("Progdir:LastCallers.DATA",MODE_OLDFILE))
  271.   {
  272.     if (CallerList=HBBS_CreateList())
  273.     {
  274.       do
  275.       {
  276.         while ((FGets(FH,buffer,1024)) && (strnicmp(buffer,"!DATA!",6)!=0));
  277.  
  278.         if (strnicmp(buffer,"!DATA!",6)==0) // if this is NOT what we want then we're at the EOF
  279.         {
  280.           if (FGets(FH,buffer,1024))
  281.           {
  282.             error=TRUE; // memory error!
  283.             stripcr(buffer);
  284.             if (Caller=(struct CallerNode *)HBBS_CreateNode(buffer,sizeof(struct CallerNode)))
  285.             {
  286.               if (FGets(FH,buffer,1024))
  287.               {
  288.                 stripcr(buffer);
  289.                 strNcpy(Caller->Group,buffer,LEN_GROUP);
  290.                 if (FGets(FH,buffer,1024))
  291.                 {
  292.                   stripcr(buffer);
  293.                   strNcpy(Caller->OnTime,buffer,LEN_TIMESTR);
  294.                   if (FGets(FH,buffer,1024))
  295.                   {
  296.                     stripcr(buffer);
  297.                     strNcpy(Caller->OffTime,buffer,LEN_TIMESTR);
  298.                     if (FGets(FH,buffer,1024))
  299.                     {
  300.                       stripcr(buffer);
  301.                       strNcpy(Caller->Date,buffer,LEN_DATESTR);
  302.                       if (FGets(FH,buffer,1024))
  303.                       {
  304.                         stripcr(buffer);
  305.                         strNcpy(Caller->Actions,buffer,LEN_ACTIONS);
  306.                         if (FGets(FH,buffer,1024))
  307.                         {
  308.                           stripcr(buffer);
  309.                           strNcpy(Caller->Speed,buffer,MAX_CPSBAUD_LEN+1);
  310.                           if (FGets(FH,buffer,1024))
  311.                           {
  312.                             stripcr(buffer);
  313.                             if (sscanf(buffer,"%ld",&Caller->NodeNum) && Caller->NodeNum>=0 && Caller->NodeNum<BBSGlobal->BBSNodes)
  314.                             {
  315.                               if (FGets(FH,buffer,1024))
  316.                               {
  317.                                 stripcr(buffer);
  318.                                 if (sscanf(buffer,"%ld",&Caller->FilesUploaded))
  319.                                 {
  320.                                   if (FGets(FH,buffer,1024))
  321.                                   {
  322.                                     stripcr(buffer);
  323.                                     if (sscanf(buffer,"%ld",&Caller->FilesDownloaded))
  324.                                     {
  325.                                       if (FGets(FH,buffer,1024))
  326.                                       {
  327.                                         stripcr(buffer);
  328.                                         if (sscanf(buffer,"%ld",&Caller->BytesUploaded))
  329.                                         {
  330.                                           if (FGets(FH,buffer,1024))
  331.                                           {
  332.                                             stripcr(buffer);
  333.                                             if (sscanf(buffer,"%ld",&Caller->BytesDownloaded))
  334.                                             {
  335.                                               AddTail(CallerList,(struct Node*)Caller);
  336.                                               error=FALSE;
  337.                                             }
  338.                                           }
  339.                                         }
  340.                                       }
  341.                                     }
  342.                                   }
  343.                                 }
  344.                               }
  345.                             }
  346.                           }
  347.                         }
  348.                       }
  349.                     }
  350.                   }        // cooooool, pretty patterns...
  351.                 }
  352.               }
  353.             }
  354.           }
  355.         }
  356.         else
  357.         {
  358.           eof=TRUE;
  359.         }
  360.       } while (!eof && !error);
  361.  
  362.       if (error)
  363.       {
  364.         FreeStrList(CallerList);
  365.       }
  366.       else
  367.       {
  368.         if (HBBS_NodesInList(CallerList))
  369.         {
  370.           retval=TRUE;
  371.         }
  372.       }
  373.     }
  374.     Close(FH);
  375.   }
  376.   return(retval);
  377. }
  378.  
  379. void UpdateDataFile(LONG NodeNum)
  380. {
  381.   LONG Nodes,ToRemove;
  382.   struct NodeData *nd;
  383.   struct CallerNode *Caller;
  384.  
  385.   if (!(nd=HBBS_NodeDataPtr(NodeNum))) return;
  386.  
  387.  
  388.   if (!(ReadDataFile(80)))  // *C* #define 80
  389.   {
  390.     CallerList=HBBS_CreateList();
  391.   }
  392.   if (CallerList)
  393.   {
  394.     Nodes=HBBS_NodesInList(CallerList);
  395.  
  396.     // make sure there are 79 nodes MAX in the list..
  397.     if (Nodes>79)
  398.     {
  399.       ToRemove=Nodes-79;
  400.  
  401.       while (ToRemove>0)
  402.       {
  403.         ToRemove--;
  404.         Caller=(struct CallerNode *)CallerList->lh_Tail; //making sure we remove the last one in the list
  405.         HBBS_FreeNode((struct Node*)Caller,TRUE);
  406.       }
  407.     }
  408.     // then add our new one..
  409.     if (nd->Status==STAT_ONLINE && nd->User.Valid)
  410.     {
  411.       if (Caller=(struct CallerNode *)HBBS_CreateNode(nd->User.CallData.Handle,sizeof(struct CallerNode)))
  412.       {
  413.         strNcpy(Caller->Group,nd->User.CallData.Group,LEN_GROUP);
  414.         strNcpy(Caller->OnTime,nd->LastCalledTime,LEN_TIMESTR);
  415.         HBBS_GetTime(Caller->OffTime);
  416.         HBBS_GetDate(Caller->Date);
  417.         strNcpy(Caller->Actions,nd->Actions,LEN_ACTIONS);
  418.         replace(Caller->Actions,Caller->Actions," ","");
  419.         strNcpy(Caller->Speed,nd->ConnectBaud,MAX_CPSBAUD_LEN+1);
  420.         Caller->NodeNum=NodeNum;
  421.         Caller->FilesUploaded=nd->User.FilesUploaded;
  422.         Caller->FilesDownloaded=nd->User.FilesDownloaded;
  423.         Caller->BytesUploaded=nd->User.BytesUploaded;
  424.         Caller->BytesDownloaded=nd->User.BytesDownloaded;
  425.         AddHead(CallerList,(struct Node*)Caller);
  426.  
  427.         SaveDataFile();
  428.       }
  429.     }
  430.     else
  431.     {
  432.       puts ("No User Online!");
  433.     }
  434.   }
  435. }
  436.  
  437. int main(int argc,char *argv[])
  438. {
  439.   LONG loop,Users,startparam=1,nodenum;
  440.  
  441.   BOOL scanall=FALSE,OK=FALSE,NoLocal=FALSE;
  442.   char *filename=NULL;
  443.  
  444.   init("LastCallers");
  445.  
  446.   if (BBSGlobal=HBBS_GimmeBBS())
  447.   {
  448.     if (argc>=2)
  449.     {
  450.       if (argv[1][0]=='?')
  451.       {
  452.         DisplayHelp();
  453.       }
  454.       else
  455.       {
  456.         if (stricmp(argv[startparam],"UPDATE")==0 && argc>2)
  457.         {
  458.           startparam++;
  459.           if (sscanf(argv[startparam],"%ld",&loop) && loop>=0 && loop<BBSGlobal->BBSNodes)
  460.           {
  461.             startparam++;
  462.             UpdateDataFile(loop); // loop=node number to get users data from..
  463.           }
  464.         }
  465.         if ((startparam<argc) && (stricmp(argv[startparam],"CREATE")==0))
  466.         {
  467.           startparam++;
  468.           filename=argv[startparam++];
  469.  
  470.           if ((startparam<argc) && (sscanf(argv[startparam++],"%ld",&Users)) && (Users))
  471.           {
  472.             if ((startparam<argc) && (stricmp(argv[startparam],"ALLNODES")==0))
  473.             {
  474.               startparam++;
  475.               scanall=TRUE;
  476.               OK=TRUE;
  477.             }
  478.             else
  479.             {
  480.               if ((startparam<argc) && (sscanf(argv[startparam],"%ld",&loop)) && (loop>=0) && (loop<BBSGlobal->BBSNodes))
  481.               {
  482.                 startparam++;
  483.                 nodenum=loop;
  484.                 OK=TRUE;
  485.               }
  486.             }
  487.             if ((startparam<argc) && (stricmp(argv[startparam],"NOLOCAL")==0))
  488.             {
  489.               NoLocal=TRUE;
  490.             }
  491.             if (OK && filename && filename[0])
  492.             {
  493.               if (ReadDataFile(Users))
  494.               {
  495.                 CreateFile(NoLocal,scanall,nodenum,Users,filename);
  496.               }
  497.               if (CallerList) FreeStrList(CallerList);
  498.             }
  499.           }
  500.         }
  501.       }
  502.     }
  503.     else
  504.     {
  505.       DisplayHelp();
  506.     }
  507.   }
  508.   else
  509.   {
  510.     puts("Control must be running!! This util requires access to runtime data!");
  511.   }
  512.   cleanup(0);
  513. }
  514.